package com.wy.myapplication

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View

/**
 * Time:8/19/21
 * Author:ypf
 * Description:手势解锁
 */
class GestureUnlock : View {


    val TAG: String = "GestureUnlock"

    var pressColor = Color.RED
    var unpressColor = Color.RED

    var pressPaint: Paint? = null
    var unpressPaint: Paint? = null
    var trianglePaint: Paint? = null

    var left1: Float = 0f
    var top1: Float = 0f
    var right1: Float = 0f
    var bottom1: Float = 0f

    var left2: Float = 0f
    var top2: Float = 0f
    var right2: Float = 0f
    var bottom2: Float = 0f

    var left3: Float = 0f
    var top3: Float = 0f
    var right3: Float = 0f
    var bottom3: Float = 0f

    var left4: Float = 0f
    var top4: Float = 0f
    var right4: Float = 0f
    var bottom4: Float = 0f

    var left5: Float = 0f
    var top5: Float = 0f
    var right5: Float = 0f
    var bottom5: Float = 0f

    var left6: Float = 0f
    var top6: Float = 0f
    var right6: Float = 0f
    var bottom6: Float = 0f

    var left7: Float = 0f
    var top7: Float = 0f
    var right7: Float = 0f
    var bottom7: Float = 0f

    var left8: Float = 0f
    var top8: Float = 0f
    var right8: Float = 0f
    var bottom8: Float = 0f

    var left9: Float = 0f
    var top9: Float = 0f
    var right9: Float = 0f
    var bottom9: Float = 0f

    var moveX: Float = 0f
    var moveY: Float = 0f

    var password: ArrayList<String> = ArrayList()

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        var typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.gestureUnlock, 0, 0)
        pressColor = typedArray.getColor(R.styleable.gestureUnlock_pressColor, Color.RED)
        unpressColor = typedArray.getColor(R.styleable.gestureUnlock_unpressColor, Color.RED)
        typedArray.recycle()
        init()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {

        var typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.gestureUnlock, defStyleAttr, 0)
        pressColor = typedArray.getColor(R.styleable.gestureUnlock_pressColor, Color.RED)
        unpressColor = typedArray.getColor(R.styleable.gestureUnlock_unpressColor, Color.RED)
        typedArray.recycle()
        init()
    }

    private fun init() {
        pressPaint = Paint()
        pressPaint!!.color = pressColor
        pressPaint!!.style = Paint.Style.STROKE
        pressPaint!!.strokeWidth = 10f

        unpressPaint = Paint()
        unpressPaint!!.color = unpressColor
        unpressPaint!!.style = Paint.Style.STROKE
        unpressPaint!!.strokeWidth = 10f

        trianglePaint = Paint()
        trianglePaint!!.color = pressColor
        trianglePaint!!.style = Paint.Style.FILL
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        //width 1440
        //height 2432

        var bigCircleRadius: Float = ((width / 6 - 10) / 2).toFloat();
        var smallCircleRadius: Float = ((width / 6 - 10) / 12).toFloat();

        var x1 = width / 3 / 2 //240
        var y1 = height / 2 - bigCircleRadius * 4 //1216
        if (password.contains("1")) {
            canvas.drawCircle(x1.toFloat(), y1, bigCircleRadius, pressPaint!!)
            canvas.drawCircle(x1.toFloat(), y1, smallCircleRadius, pressPaint!!)
        } else {
            canvas.drawCircle(x1.toFloat(), y1, bigCircleRadius, unpressPaint!!)
            canvas.drawCircle(x1.toFloat(), y1, smallCircleRadius, unpressPaint!!)
        }

        left1 = x1 - bigCircleRadius
        top1 = y1 - bigCircleRadius
        right1 = x1 + bigCircleRadius
        bottom1 = y1 + bigCircleRadius

        var x2 = width / 2 //240
        var y2 = height / 2 - bigCircleRadius * 4 //1216
        if (password.contains("2")) {
            canvas.drawCircle(x2.toFloat(), y2, bigCircleRadius, pressPaint!!)
            canvas.drawCircle(x2.toFloat(), y2, smallCircleRadius, pressPaint!!)
        } else {
            canvas.drawCircle(x2.toFloat(), y2, bigCircleRadius, unpressPaint!!)
            canvas.drawCircle(x2.toFloat(), y2, smallCircleRadius, unpressPaint!!)
        }

        left2 = x2 - bigCircleRadius
        top2 = y2 - bigCircleRadius
        right2 = x2 + bigCircleRadius
        bottom2 = y2 + bigCircleRadius

        var x3 = width - width / 3 / 2 //240
        var y3 = height / 2 - bigCircleRadius * 4 //1216
        if (password.contains("3")) {
            canvas.drawCircle(x3.toFloat(), y3, bigCircleRadius, pressPaint!!)
            canvas.drawCircle(x3.toFloat(), y3, smallCircleRadius, pressPaint!!)
        } else {
            canvas.drawCircle(x3.toFloat(), y3, bigCircleRadius, unpressPaint!!)
            canvas.drawCircle(x3.toFloat(), y3, smallCircleRadius, unpressPaint!!)
        }

        left3 = x3 - bigCircleRadius
        top3 = y3 - bigCircleRadius
        right3 = x3 + bigCircleRadius
        bottom3 = y3 + bigCircleRadius

        var x4 = width / 3 / 2 //240
        var y4 = height / 2 //1216
        if (password.contains("4")) {
            canvas.drawCircle(x4.toFloat(), y4.toFloat(), bigCircleRadius, pressPaint!!)
            canvas.drawCircle(x4.toFloat(), y4.toFloat(), smallCircleRadius, pressPaint!!)
        } else {
            canvas.drawCircle(x4.toFloat(), y4.toFloat(), bigCircleRadius, unpressPaint!!)
            canvas.drawCircle(x4.toFloat(), y4.toFloat(), smallCircleRadius, unpressPaint!!)
        }

        left4 = x4 - bigCircleRadius
        top4 = y4 - bigCircleRadius
        right4 = x4 + bigCircleRadius
        bottom4 = y4 + bigCircleRadius

        var x5 = width / 2 //240
        var y5 = height / 2 //1216
        if (password.contains("5")) {
            canvas.drawCircle(x5.toFloat(), y5.toFloat(), bigCircleRadius, pressPaint!!)
            canvas.drawCircle(x5.toFloat(), y5.toFloat(), smallCircleRadius, pressPaint!!)
        } else {
            canvas.drawCircle(x5.toFloat(), y5.toFloat(), bigCircleRadius, unpressPaint!!)
            canvas.drawCircle(x5.toFloat(), y5.toFloat(), smallCircleRadius, unpressPaint!!)
        }

        left5 = x5 - bigCircleRadius
        top5 = y5 - bigCircleRadius
        right5 = x5 + bigCircleRadius
        bottom5 = y5 + bigCircleRadius

        var x6 = width - x4 //240
        var y6 = height / 2 //1216
        if (password.contains("6")) {
            canvas.drawCircle(x6.toFloat(), y6.toFloat(), bigCircleRadius, pressPaint!!)
            canvas.drawCircle(x6.toFloat(), y6.toFloat(), smallCircleRadius, pressPaint!!)
        } else {
            canvas.drawCircle(x6.toFloat(), y6.toFloat(), bigCircleRadius, unpressPaint!!)
            canvas.drawCircle(x6.toFloat(), y6.toFloat(), smallCircleRadius, unpressPaint!!)
        }

        left6 = x6 - bigCircleRadius
        top6 = y6 - bigCircleRadius
        right6 = x6 + bigCircleRadius
        bottom6 = y6 + bigCircleRadius

        var x7 = width / 3 / 2 //240
        var y7 = height / 2 + bigCircleRadius * 4 //1216
        if (password.contains("7")) {
            canvas.drawCircle(x7.toFloat(), y7, bigCircleRadius, pressPaint!!)
            canvas.drawCircle(x7.toFloat(), y7, smallCircleRadius, pressPaint!!)
        } else {
            canvas.drawCircle(x7.toFloat(), y7, bigCircleRadius, unpressPaint!!)
            canvas.drawCircle(x7.toFloat(), y7, smallCircleRadius, unpressPaint!!)
        }

        left7 = x7 - bigCircleRadius
        top7 = y7 - bigCircleRadius
        right7 = x7 + bigCircleRadius
        bottom7 = y7 + bigCircleRadius

        var x8 = width / 2 //240
        var y8 = height / 2 + bigCircleRadius * 4 //1216
        if (password.contains("8")) {
            canvas.drawCircle(x8.toFloat(), y8, bigCircleRadius, pressPaint!!)
            canvas.drawCircle(x8.toFloat(), y8, smallCircleRadius, pressPaint!!)
        } else {
            canvas.drawCircle(x8.toFloat(), y8, bigCircleRadius, unpressPaint!!)
            canvas.drawCircle(x8.toFloat(), y8, smallCircleRadius, unpressPaint!!)
        }

        left8 = x8 - bigCircleRadius
        top8 = y8 - bigCircleRadius
        right8 = x8 + bigCircleRadius
        bottom8 = y8 + bigCircleRadius

        var x9 = width - width / 3 / 2 //240
        var y9 = height / 2 + bigCircleRadius * 4 //1216
        if (password.contains("9")) {
            canvas.drawCircle(x9.toFloat(), y9, bigCircleRadius, pressPaint!!)
            canvas.drawCircle(x9.toFloat(), y9, smallCircleRadius, pressPaint!!)
        } else {
            canvas.drawCircle(x9.toFloat(), y9, bigCircleRadius, unpressPaint!!)
            canvas.drawCircle(x9.toFloat(), y9, smallCircleRadius, unpressPaint!!)
        }

        left9 = x9 - bigCircleRadius
        top9 = y9 - bigCircleRadius
        right9 = x9 + bigCircleRadius
        bottom9 = y9 + bigCircleRadius

        for (i in password.indices) {
            if (0 == i) {
                continue
            }
            var index = password.get(i)
            var x: Int? = 0
            var y: Int? = 0
            when (index) {
                "1" -> {
                    x = x1
                    y = y1.toInt()
                }
                "2" -> {
                    x = x2
                    y = y2.toInt()
                }
                "3" -> {
                    x = x3
                    y = y3.toInt()
                }
                "4" -> {
                    x = x4
                    y = y4.toInt()
                }
                "5" -> {
                    x = x5
                    y = y5.toInt()
                }
                "6" -> {
                    x = x6
                    y = y6.toInt()
                }
                "7" -> {
                    x = x7
                    y = y7.toInt()
                }
                "8" -> {
                    x = x8
                    y = y8.toInt()
                }
                "9" -> {
                    x = x9
                    y = y9.toInt()
                }
            }
            var lastIndex = password.get(i - 1)
            var lastX: Int? = 0
            var lastY: Int? = 0
            when (lastIndex) {
                "1" -> {
                    lastX = x1
                    lastY = y1.toInt()
                }
                "2" -> {
                    lastX = x2
                    lastY = y2.toInt()
                }
                "3" -> {
                    lastX = x3
                    lastY = y3.toInt()
                }
                "4" -> {
                    lastX = x4
                    lastY = y4.toInt()
                }
                "5" -> {
                    lastX = x5
                    lastY = y5.toInt()
                }
                "6" -> {
                    lastX = x6
                    lastY = y6.toInt()
                }
                "7" -> {
                    lastX = x7
                    lastY = y7.toInt()
                }
                "8" -> {
                    lastX = x8
                    lastY = y8.toInt()
                }
                "9" -> {
                    lastX = x9
                    lastY = y9.toInt()
                }
            }

            //连线
            canvas.drawLine(lastX!!.toFloat(), lastY!!.toFloat(), x!!.toFloat(), y!!.toFloat(), pressPaint!!)

            //画三角形
            //从左向右箭头
            if (x > lastX && y == lastY) {
                //画三角形
                var path = Path()
                var triangleX: Int = (x!! - lastX!!) / 2 + lastX
                var triangleY: Int = lastY!!
                path.moveTo(triangleX.toFloat(), triangleY.toFloat())
                path.lineTo(triangleX.toFloat() - 30, triangleY.toFloat() - 15)
                path.lineTo(triangleX.toFloat() - 30, triangleY.toFloat() + 15)
                path.close()
                canvas.drawPath(path, trianglePaint!!)
            } else if (x > lastX && y > lastY) {
                //画三角形
                var path = Path()
                var triangleX: Int = (x!! - lastX!!) / 2 + lastX
                var triangleY: Int = (y!! - lastY!!) / 2 + lastY
                path.moveTo(triangleX.toFloat(), triangleY.toFloat())
                path.lineTo(triangleX.toFloat() - 10, triangleY.toFloat() - 30)
                path.lineTo(triangleX.toFloat() - 30, triangleY.toFloat() - 10)
                path.close()
                canvas.drawPath(path, trianglePaint!!)
            } else if (x < lastX && y < lastY) {
                //画三角形
                var path = Path()
                var triangleX: Int = (lastX!! - x!!) / 2 + x
                var triangleY: Int = (lastY!! - y!!) / 2 + y
                path.moveTo(triangleX.toFloat(), triangleY.toFloat())
                path.lineTo(triangleX.toFloat() + 10, triangleY.toFloat() + 30)
                path.lineTo(triangleX.toFloat() + 30, triangleY.toFloat() + 10)
                path.close()
                canvas.drawPath(path, trianglePaint!!)
            } else if (x > lastX && y < lastY) {
                //画三角形
                var path = Path()
                var triangleX: Int = (lastX!! - x!!) / 2 + x
                var triangleY: Int = (lastY!! - y!!) / 2 + y
                path.moveTo(triangleX.toFloat(), triangleY.toFloat())
                path.lineTo(triangleX.toFloat() - 30, triangleY.toFloat() + 5)
                path.lineTo(triangleX.toFloat() - 5, triangleY.toFloat() + 30)
                path.close()
                canvas.drawPath(path, trianglePaint!!)
            } else if (x < lastX && y > lastY) {
                //画三角形
                var path = Path()
                var triangleX: Int = (x!! - lastX!!) / 2 + lastX
                var triangleY: Int = (y!! - lastY!!) / 2 + lastY
                path.moveTo(triangleX.toFloat(), triangleY.toFloat())
                path.lineTo(triangleX.toFloat() + 30, triangleY.toFloat() - 5)
                path.lineTo(triangleX.toFloat() + 5, triangleY.toFloat() - 30)
                path.close()
                canvas.drawPath(path, trianglePaint!!)
            } else if (x < lastX && y == lastY) {
                //画三角形
                var path = Path()
                var triangleX: Int = (lastX!! - x!!) / 2 + x
                var triangleY: Int = lastY!!
                path.moveTo(triangleX.toFloat(), triangleY.toFloat())
                path.lineTo(triangleX.toFloat() + 30, triangleY.toFloat() + 15)
                path.lineTo(triangleX.toFloat() + 30, triangleY.toFloat() - 15)
                path.close()
                canvas.drawPath(path, trianglePaint!!)
            } else if (x == lastX && y > lastY) {
                //画三角形
                var path = Path()
                var triangleX: Int = x
                var triangleY: Int = (y!! - lastY!!) / 2 + lastY
                path.moveTo(triangleX.toFloat(), triangleY.toFloat())
                path.lineTo(triangleX.toFloat() - 15, triangleY.toFloat() - 30)
                path.lineTo(triangleX.toFloat() + 15, triangleY.toFloat() - 30)
                path.close()
                canvas.drawPath(path, trianglePaint!!)
            } else if (x == lastX && y < lastY) {
                //画三角形
                var path = Path()
                var triangleX: Int = x
                var triangleY: Int = (lastY!! - y!!) / 2 + y
                path.moveTo(triangleX.toFloat(), triangleY.toFloat())
                path.lineTo(triangleX.toFloat() - 15, triangleY.toFloat() + 30)
                path.lineTo(triangleX.toFloat() + 15, triangleY.toFloat() + 30)
                path.close()
                canvas.drawPath(path, trianglePaint!!)
            }

        }

        //跟随鼠标的线
        if (!password.isEmpty()) {

            var index = password.get(password.size - 1)
            var x: Int? = 0
            var y: Int? = 0
            when (index) {
                "1" -> {
                    x = x1
                    y = y1.toInt()
                }
                "2" -> {
                    x = x2
                    y = y2.toInt()
                }
                "3" -> {
                    x = x3
                    y = y3.toInt()
                }
                "4" -> {
                    x = x4
                    y = y4.toInt()
                }
                "5" -> {
                    x = x5
                    y = y5.toInt()
                }
                "6" -> {
                    x = x6
                    y = y6.toInt()
                }
                "7" -> {
                    x = x7
                    y = y7.toInt()
                }
                "8" -> {
                    x = x8
                    y = y8.toInt()
                }
                "9" -> {
                    x = x9
                    y = y9.toInt()
                }
            }

            canvas.drawLine(x!!.toFloat(), y!!.toFloat(), moveX!!.toFloat(), moveY!!.toFloat(), pressPaint!!)
        }

    }

    override fun onTouchEvent(event: MotionEvent): Boolean {

        var x: Float = event.x
        var y: Float = event.y

        when (event.action) {

            MotionEvent.ACTION_DOWN -> {

            }
            MotionEvent.ACTION_MOVE -> {

            }
            MotionEvent.ACTION_UP -> {
                postDelayed(Runnable {
                    Log.i(TAG, "password:" + password.toString())
                    password.clear()
                    invalidate()
                }, 2 * 1000)
            }
        }

        var currentIndex: String

        if (x > left1 && x < right1 && y > top1 && y < bottom1) {
            currentIndex = "1"
            if (password.isEmpty()) {
                password.add(currentIndex)
            } else if (!password.get(password.size - 1).equals(currentIndex)) {
                password.add(currentIndex)
            }
        } else if (x > left2 && x < right2 && y > top2 && y < bottom2) {
            currentIndex = "2"
            if (password.isEmpty()) {
                password.add(currentIndex)
            } else if (!password.get(password.size - 1).equals(currentIndex)) {
                password.add(currentIndex)
            }
        } else if (x > left3 && x < right3 && y > top3 && y < bottom3) {
            currentIndex = "3"
            if (password.isEmpty()) {
                password.add(currentIndex)
            } else if (!password.get(password.size - 1).equals(currentIndex)) {
                password.add(currentIndex)
            }
        } else if (x > left4 && x < right4 && y > top4 && y < bottom4) {
            currentIndex = "4"
            if (password.isEmpty()) {
                password.add(currentIndex)
            } else if (!password.get(password.size - 1).equals(currentIndex)) {
                password.add(currentIndex)
            }
        } else if (x > left5 && x < right5 && y > top5 && y < bottom5) {
            currentIndex = "5"
            if (password.isEmpty()) {
                password.add(currentIndex)
            } else if (!password.get(password.size - 1).equals(currentIndex)) {
                password.add(currentIndex)
            }
        } else if (x > left6 && x < right6 && y > top6 && y < bottom6) {
            currentIndex = "6"
            if (password.isEmpty()) {
                password.add(currentIndex)
            } else if (!password.get(password.size - 1).equals(currentIndex)) {
                password.add(currentIndex)
            }
        } else if (x > left7 && x < right7 && y > top7 && y < bottom7) {
            currentIndex = "7"
            if (password.isEmpty()) {
                password.add(currentIndex)
            } else if (!password.get(password.size - 1).equals(currentIndex)) {
                password.add(currentIndex)
            }
        } else if (x > left8 && x < right8 && y > top8 && y < bottom8) {
            currentIndex = "8"
            if (password.isEmpty()) {
                password.add(currentIndex)
            } else if (!password.get(password.size - 1).equals(currentIndex)) {
                password.add(currentIndex)
            }
        } else if (x > left9 && x < right9 && y > top9 && y < bottom9) {
            currentIndex = "9"
            if (password.isEmpty()) {
                password.add(currentIndex)
            } else if (!password.get(password.size - 1).equals(currentIndex)) {
                password.add(currentIndex)
            }
        }

        moveX = event.x
        moveY = event.y

        invalidate()

        return true
    }

}

